package work.coffee.gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import work.coffee.connect.ConnectionFactory;
import work.coffee.dao.CoffeeDao;
import work.coffee.dto.Coffee;




public class CoffeeFrame extends JFrame implements ActionListener {
	private JPanel contentPane;
	private JTextField tfNum;
	private JTextField tfName;
	private JTextField tfPrice;
	private JTextField tfMargin;
	private JTextField tfCount;
	private JButton btnPrn1;
	private JButton btnPrn2;
	private CoffeeDialog dialog_coffee;
	protected CoffeeDao dao;
	protected Connection con;
	private JButton btnInput;
	private Coffee cf;
	private String name;
	private String num;
	private int price;
	private int count;
	private int margin;
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Connection con = ConnectionFactory.getConnection();
					CoffeeFrame frame = new CoffeeFrame(con);
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	public CoffeeFrame(Connection con) {
		this.con = con;
		dao = new CoffeeDao(con);//
		initialize();
	}
	private void initialize() {
		setTitle("커피전문점");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 400, 220);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(5, 5));
		JPanel panelContent = new JPanel();
		contentPane.add(panelContent, BorderLayout.NORTH);
		panelContent.setLayout(new GridLayout(5, 2, 5, 5));
		JLabel lblNewLabel = new JLabel("제품코드 : ");
		lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
		panelContent.add(lblNewLabel);
		tfNum = new JTextField();
		tfNum.addActionListener(this);
		panelContent.add(tfNum);
		tfNum.setColumns(10);
		JLabel lblNewLabel_1 = new JLabel("제품명 : ");
		lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
		panelContent.add(lblNewLabel_1);
		tfName = new JTextField();
		tfName.setEditable(false);
		panelContent.add(tfName);
		tfName.setColumns(10);
		JLabel lblNewLabel_2 = new JLabel("제품단가 : ");
		lblNewLabel_2.setHorizontalAlignment(SwingConstants.RIGHT);
		panelContent.add(lblNewLabel_2);
		tfPrice = new JTextField();
		panelContent.add(tfPrice);
		tfPrice.setColumns(8);
		JLabel label = new JLabel("판매수량 : ");
		label.setHorizontalAlignment(SwingConstants.RIGHT);
		panelContent.add(label);
		tfCount = new JTextField();
		panelContent.add(tfCount);
		tfCount.setColumns(10);
		JLabel lblNewLabel_3 = new JLabel("마진율 : ");
		lblNewLabel_3.setHorizontalAlignment(SwingConstants.RIGHT);
		panelContent.add(lblNewLabel_3);
		tfMargin = new JTextField();
		panelContent.add(tfMargin);
		tfMargin.setColumns(10);
		JPanel panelButton = new JPanel();
		contentPane.add(panelButton, BorderLayout.CENTER);
		panelButton.setLayout(new GridLayout(0, 3, 5, 5));
		btnInput = new JButton("입력");
		btnInput.addActionListener(this);
		panelButton.add(btnInput);
		btnPrn1 = new JButton("출력1");
		btnPrn1.addActionListener(this);
		panelButton.add(btnPrn1);
		btnPrn2 = new JButton("출력2");
		btnPrn2.addActionListener(this);
		panelButton.add(btnPrn2);
	}
	public void actionPerformed(ActionEvent e)   {
		cf = new Coffee();
		cf.setcNum(tfNum.getText());
		if (e.getSource() == tfNum) {
			String sh = dao.searchName(cf);
			if(sh!=null){
				tfName.setText(sh);				
			}
			else{
				
			}
		}
		else if (e.getSource() == btnInput) {
			num = tfNum.getText().trim();
			name = tfName.getText().trim();
			try{
				price = Integer.parseInt(tfPrice.getText());
				count = Integer.parseInt(tfCount.getText());
				margin = Integer.parseInt(tfMargin.getText());
			}catch(NumberFormatException e2){
				JOptionPane.showMessageDialog(this, "입력실패");
			}
			cf = new Coffee(num, price, count, margin);
			cf.setcName(name);
			try {
				dao.insertCoffee(cf);
				JOptionPane.showConfirmDialog(this, "입력완료");
			} catch (SQLException e1) {
				JOptionPane.showMessageDialog(this, "입력실패");
			} 
		}
		else if(e.getSource()==btnPrn1){
		try {
				prnListOrderBy(true);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}			
		}
		else if(e.getSource()==btnPrn2){
			try {
				prnListOrderBy(false);
			} catch (SQLException e1) {
				e1.printStackTrace();
			}		
		}			
	}
	private void prnListOrderBy(boolean b) throws SQLException {
		ArrayList<Coffee> temp = dao.listItem();
		if(b){
			Collections.sort(temp, new Comparator<Coffee>() {
				@Override
				public int compare(Coffee o1, Coffee o2) {
					return o2.sellingPrice()-o1.sellingPrice();
				}
			});
			dialog_coffee = new CoffeeDialog("btnPrn1",temp);
			dialog_coffee.show();
		}
		else{
			Collections.sort(temp, new Comparator<Coffee>() {
				@Override
				public int compare(Coffee o1, Coffee o2) {
					return (int) (o2.marginRate()-o1.marginRate());
				}
			});
			dialog_coffee = new CoffeeDialog("btnPrn2",temp);
			dialog_coffee.show();
		}		
	}
}
